﻿Imports LinhKienMayTinh.CommonMethods

Public Class ProductList
    Inherits BasePage

#Region "Khao báo các biến toàn cục"

    Protected dsSource As DataSet

#End Region

#Region "Khai báo các event"

    ''' <summary>
    ''' Page_Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xCategory As String = String.Empty
        Dim xManufactory As String = String.Empty
        Dim nPage As Integer = 1
        Dim nProductPerPage As Integer = 1
        Dim dsProductList As DataSet
        Dim xCategoryName As String = String.Empty
        Dim xManufactoryName As String = String.Empty
        Dim nTotalPage As Integer = 0

        Try
            xCategory = Me.QueryString("category")
            xManufactory = Me.QueryString("manufactory")
            nPage = Nz(Me.QueryString("page"), 1)
            nProductPerPage = Nz(AppSettings("ProductPerPage"), 12)
            nTotalPage = Math.Ceiling(GetProductCount(xCategory, xManufactory) / nProductPerPage)

            If nTotalPage > 0 AndAlso nPage > nTotalPage Then
                Throw New Exception
            End If

            Dim dsLinkName As DataSet = GetLinkNameInfo(xCategory, xManufactory)
            If dsLinkName.Tables(0).Rows.Count <> 2 Then
                Throw New Exception
            Else
                xCategoryName = dsLinkName.Tables(0).Rows(0).Item("NAME")
                xManufactoryName = dsLinkName.Tables(0).Rows(1).Item("NAME")

                Me.Header.Title = Me.AppSettings("SiteName") & " | " & (xCategoryName & " - " & xManufactoryName)
                Me.lbTitle.Text = (xCategoryName & " - " & xManufactoryName)
            End If
            dsProductList = GetProductList(xCategory, xManufactory, nPage, nProductPerPage)
            If dsProductList IsNot Nothing AndAlso dsProductList.Tables(0).Rows.Count > 0 Then

                Me.ucProductList.DataSource = dsProductList
                If IsNothing(Session(CommonConstants.C_USERNAME)) = False Then
                    Me.ucProductList.HasEditButton = True
                End If

                ucPagingBar.Url = ResolveUrl("~/" & xCategory & "/" & xManufactory)
                ucPagingBar.CurrentPage = nPage
                ucPagingBar.TotalPage = nTotalPage

                Me.DataBind()
            End If

            '' Tạo động meta tag Description và Keywords.
            Dim meta As HtmlMeta = Page.Header.FindControl("Keywords")
            If meta IsNot Nothing Then
                meta.Content = xCategoryName & ", " & xManufactoryName
            Else
                meta = New HtmlMeta
                meta.Name = "Keywords"
                meta.ID = "Keywords"
                meta.Content = xCategoryName & ", " & xManufactoryName
                Page.Header.Controls.Add(meta)
            End If
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

    Private Sub ProductList_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Try
            Dim master As LinhKienMayTinhMasterPage = DirectCast(Me.Page.Master, LinhKienMayTinhMasterPage)            
            master.CssLinkAdd(ResolveUrl("~/Style/ProductListMetroStyle.css"))
            master.CssLinkAdd(ResolveUrl("~/Style/PagingBarMetroStyle.css"))
            master.ScriptLinkAdd(ResolveUrl("~/Scripts/ProductList.js"))
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

#End Region

#Region "Khai báo các function method"

    Private Function GetProductList(pCategory As String, pManufactory As String, pPage As Integer, pProductPerPage As Integer) As DataSet

        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess
        Dim nStart As Integer
        Dim nEnd As Integer

        Try
            dataAccess = Me.GetDBAccess()

            nStart = (pPage * pProductPerPage) - (pProductPerPage - 1)
            nEnd = pPage * pProductPerPage

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        DAT.*"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "("
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        ROW_NUMBER() OVER(ORDER BY T_PRODUCT.CODE,T_PRODUCT.NAME) AS ROW_NUM,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.NAME         AS CATEGORY_NAME,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.NAME      AS MANUFACTORY_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND		T_CATEGORY.LINK_NAME	= @CATEGORY"
            xSql = xSql & vbCrLf & "AND		T_MANUFACTORY.LINK_NAME = @MANUFACTORY"
            xSql = xSql & vbCrLf & ") DAT"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        DAT.ROW_NUM BETWEEN @START AND @END"

            xSql = Replace(xSql, "@CATEGORY", ToSqlStr(pCategory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@MANUFACTORY", ToSqlStr(pManufactory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@START", ToSqlStr(nStart, SqlDataType.DataType_Integer))
            xSql = Replace(xSql, "@END", ToSqlStr(nEnd, SqlDataType.DataType_Integer))

            ds = dataAccess.GetDBDataset(xSql)

            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetProductCount(pCategory As String, pManufactory As String) As Integer
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = Me.GetDBAccess()

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT COUNT(*) as ROW_COUNT"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND		T_CATEGORY.LINK_NAME	= @CATEGORY"
            xSql = xSql & vbCrLf & "AND		T_MANUFACTORY.LINK_NAME = @MANUFACTORY"

            xSql = Replace(xSql, "@CATEGORY", ToSqlStr(pCategory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@MANUFACTORY", ToSqlStr(pManufactory, SqlDataType.DataType_String))

            ds = dataAccess.GetDBDataset(xSql)

            If IsNothing(ds) OrElse ds.Tables(0).Rows.Count < 1 Then
                Throw New Exception("NO_DATA_FOUND")
            End If

            Return CInt(ds.Tables(0).Rows(0)("ROW_COUNT"))
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Function GetLinkNameInfo(ByVal pCategory As String, ByVal pManufactory As String) As DataSet

        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = Me.GetDBAccess()

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "	C.CODE"
            xSql = xSql & vbCrLf & ",	C.NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "	T_CATEGORY C"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "	C.LINK_NAME = @CATEGORY"
            xSql = xSql & vbCrLf & "UNION ALL"
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "	M.CODE"
            xSql = xSql & vbCrLf & ",	M.NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "	T_MANUFACTORY M"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "	M.LINK_NAME = @MANUFACTORY"

            xSql = Replace(xSql, "@CATEGORY", ToSqlStr(pCategory, SqlDataType.DataType_String))
            xSql = Replace(xSql, "@MANUFACTORY", ToSqlStr(pManufactory, SqlDataType.DataType_String))

            ds = dataAccess.GetDBDataset(xSql)

            If IsNothing(ds) OrElse ds.Tables(0).Rows.Count < 1 Then
                Throw New Exception("NO_DATA_FOUND")
            End If

            Return ds
        Catch ex As Exception
            Throw
        End Try
    End Function

#End Region

End Class